package com.wokongkey.bootroom.suanfa.waibupaixu;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class SortFile {

    public static void main(String[] args) throws IOException {
        File file = new File("D:/gylcodes/gyltest/suanfatestdata/source.txt");
        BufferedReader fr = new BufferedReader(new FileReader(file));
        final int SIZE = 10000;
        int[] nums = new int[SIZE];
        List<String> fileNames = new ArrayList<>();
        int index = 0;
        int test = 0;
        while (true) {
            System.out.println(test++);
            String num = fr.readLine();
            if (num == null) {//如果读取完毕后，进行一次排序并保存
                fileNames.add(sortAndSave(nums, index));
                break;
            }
            nums[index] = Integer.valueOf(num);
            index++;
            if (index == SIZE) {
                fileNames.add(sortAndSave(nums, index));//sortAndSave是将nums中前index条记录先快速排序，然后存入文件，最好将文件名返回。
                index = 0;//重置index
            }

            fr.close();

            mergeSort(fileNames);//将所有fileNames的文件进行合并
        }


    }

    public static String sortAndSave(int[] nums, int size) throws IOException {
        QuickSort.sort(nums, 0, size - 1);
        String fileName = "D:\\gylcodes\\gyltest\\suanfatestdata\\sort" + System.nanoTime() + ".txt";
        File rf = new File(fileName);
        BufferedWriter bw = new BufferedWriter(new FileWriter(rf));
        for (int i = 0; i < nums.length; i++) {
            bw.write(nums[i] + "\n");
        }
        bw.close();
        return fileName;
    }

    public static void mergeSort(List<String> fileNames) throws IOException {
        List<String> tempFileNames = new ArrayList<String>();
        for (int i = 0; i < fileNames.size(); i++) {
            String resultFileName = "D:\\gylcodes\\gyltest\\suanfatestdata\\sort" + System.nanoTime() + ".txt";
            File resultFile = new File(resultFileName);
            tempFileNames.add(resultFileName);
            BufferedWriter bw = new BufferedWriter(new FileWriter(resultFile));

            File file1 = new File(fileNames.get(i++));
            BufferedReader br1 = new BufferedReader(new FileReader(file1));
            if (i < fileNames.size()) {
                File file2 = new File(fileNames.get(i));
                BufferedReader br2 = new BufferedReader(new FileReader(file2));
                int num1 = 0;
                int num2 = 0;
                boolean isFirst = true;
                boolean firstNext = true;
                String numVal1 = null, numVal2 = null;
                for (; ; ) {
                    if (isFirst) {
                        numVal1 = br1.readLine();
                        numVal2 = br2.readLine();
                        num1 = Integer.valueOf(numVal1);
                        num2 = Integer.valueOf(numVal2);
                        isFirst = false;
                    } else if (firstNext) {
                        numVal1 = br1.readLine();
                    } else {
                        numVal2 = br2.readLine();
                    }
                    if (numVal1 != null && numVal2 != null) {
                        if (firstNext) {
                            num1 = Integer.valueOf(numVal1);
                        } else {
                            num2 = Integer.valueOf(numVal2);
                        }
                        if (num1 < num2) {
                            bw.write(num1 + "\n");
                            firstNext = true;
                        } else {
                            bw.write(num2 + "\n");
                            firstNext = false;
                        }
                    } else {
                        if (numVal1 != null)
                            bw.write(numVal1 + "\n");
                        if (numVal2 != null)
                            bw.write(numVal2 + "\n");
                        break;
                    }
                }
                while (true) {
                    numVal2 = br2.readLine();
                    ;
                    if (numVal2 != null)
                        bw.write(numVal2 + "\n");
                    else
                        break;
                }
                br2.close();
                file2.delete();
            }
            while (true) {
                String numVal1 = br1.readLine();
                if (numVal1 != null) {
                    bw.write(numVal1 + "\n");
                } else break;
            }
            br1.close();
            file1.delete();
            bw.close();
        }
        int size = tempFileNames.size();
        if (size > 1) {
            mergeSort(tempFileNames);
        } else if (size == 1) {
            File file = new File(tempFileNames.get(0));
            file.renameTo(new File("D:\\gylcodes\\gyltest\\suanfatestdata\\result.txt"));
        }
    }
}

